package com.monkey.core.domain.user;


import com.monkey.core.domain.BaseEntity;
import com.monkey.core.domain.organization.Organization;
import com.monkey.core.plugin.dto.MonkeyDto;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.util.Date;

/**
 * Created by silentwu on 2015/6/24.
 */

@Entity
@Table(name = "sys_user")
@DynamicUpdate
@DynamicInsert
public class User extends BaseEntity<Long> {
    public static final String EMAIL_PATTERN = "^((([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+(\\.([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+)*)|((\\x22)((((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(([\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(\\\\([\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF]))))*(((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(\\x22)))@((([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.)+(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.?";
    public static final String MOBILE_PATTERN = "^0{0,1}(13[0-9]|15[0-9]|14[0-9]|18[0-9])[0-9]{8}$";
    public static final String ACCOUNT_PATTERN = "[a-zA-Z][a-zA-Z0-9_]*$";
    public static final int ACCOUNT_MIN_LENGTH = 5;
    public static final int ACCOUNT_MAX_LENGTH = 10;
    public static final int PASSWORD_MIN_LENGTH = 5;
    public static final int PASSWORD_MAX_LENGTH = 15;
    public static final int NICK_MIN_LENGTH = 5;
    public static final int NICK_MAX_LENGTH = 10;

    @Column(unique = true)
    private String account;

    private String password;

    private String nick;

    @Column(unique = true)
    private String email;

    @Column(unique = true)
    private String mobile;

    @Column(name = "status_")
    @Enumerated(EnumType.STRING)
    private UserStatus status = UserStatus.NORMAL;

    @Enumerated(EnumType.STRING)
    private Sex sex = Sex.MAN;

    @Column(name = "card_id")
    private String cardId;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birthday;

    private String QQ;
    private String address;
    private String remark;
    /**
     * 是否是管理员
     */
    private Boolean admin = false;

    @ManyToOne
    @JoinColumn(name = "organization_id")
    private Organization organization;


    public User() {
    }

    //===============================================================


    //===============================================================


    public Sex getSex() {
        return sex;
    }

    public void setSex(Sex sex) {
        this.sex = sex;
    }

    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getQQ() {
        return QQ;
    }

    public void setQQ(String QQ) {
        this.QQ = QQ;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Organization getOrganization() {
        return organization;
    }

    public void setOrganization(Organization organization) {
        this.organization = organization;
    }

    public Boolean getAdmin() {
        return admin;
    }

    public void setAdmin(Boolean admin) {
        this.admin = admin;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public UserStatus getStatus() {
        return status;
    }

    public void setStatus(UserStatus status) {
        this.status = status;
    }


    public static User createFromMonkeyDto(MonkeyDto monkeyDto) {
        User user = new User();
        user.setAccount(monkeyDto.getString("account"));
        user.setAddress(monkeyDto.getString("address"));
        user.setBirthday(monkeyDto.getDate("birthday"));
        user.setCardId(monkeyDto.getString("cardId"));
        user.setEmail(monkeyDto.getString("email"));
        user.setMobile(monkeyDto.getString("mobile"));
        user.setNick(monkeyDto.getString("nick"));
        user.setQQ(monkeyDto.getString("QQ"));
        user.setSex(Sex.valueOf(monkeyDto.getString("sex")));
        user.setStatus(UserStatus.valueOf(monkeyDto.getString("status")));
        user.setRemark(monkeyDto.getString("remark"));
        return user;
    }

    public static void modifyFromMonkeyDto(User user, MonkeyDto monkeyDto) {
        user.setAccount(monkeyDto.getString("account"));
        user.setAddress(monkeyDto.getString("address"));
        user.setBirthday(monkeyDto.getDate("birthday"));
        user.setCardId(monkeyDto.getString("cardId"));
        user.setEmail(monkeyDto.getString("email"));
        user.setMobile(monkeyDto.getString("mobile"));
        user.setNick(monkeyDto.getString("nick"));
        user.setQQ(monkeyDto.getString("QQ"));
        user.setSex(Sex.valueOf(monkeyDto.getString("sex")));
        user.setStatus(UserStatus.valueOf(monkeyDto.getString("status")));
        user.setRemark(monkeyDto.getString("remark"));
    }

}
